﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;

namespace CoreEx.DynamicInvoker.Implementation
{
    public abstract class FieldFactoryBase : DynamicFactoryBase<FieldInfo>
    {
        protected override DynamicMethod CreateDynamicMethod(FieldInfo member, bool skipVisibility)
        {
            return base.CreateDynamicMethod(member, true);
        }
        
        protected override void LoadInstance(ILGenerator iLGenerator, FieldInfo member)
        {
            if (!member.IsStatic)
            {
                iLGenerator.Emit(OpCodes.Ldarg_0);                
                if (member.DeclaringType.IsValueType)
                    iLGenerator.Emit(OpCodes.Unbox, member.DeclaringType);
            }
        }
        public override Type GetReturnType(FieldInfo member)
        {
            return member.FieldType;
        }        
    }
}
